home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / ToolBarSpacer.java < prev    next >
Text File  |  1998-08-21  |  10KB  |  340 lines

  1. package symantec.itools.awt.util;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Component;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.beans.PropertyVetoException;
  8. import java.beans.PropertyChangeEvent;
  9. import java.beans.VetoableChangeListener;
  10. import java.beans.PropertyChangeListener;
  11. import java.util.ResourceBundle;
  12.  
  13. //     01/29/97    TWB    Integrated changes from Macintosh
  14. //     07/24/97    LAB    Updated version to 1.1.  Added Space prperty.  Added support
  15. //                    for ToolBarPanel's Orientation property.  Removed unneeded
  16. //                    paint override.
  17. //     08/25/97    LAB    Now we get the parent of the parent in getPreferredSize which allows us to
  18. //                    properly get the ToolBarPanel that contains us (Addresses Mac Bug #7543).
  19.  
  20. /**
  21.  * ToolBarPanelSpacer component.
  22.  * This component is used to space items in a ToolBarPanel.
  23.  * @see symantec.itools.awt.util.ToolBarPanel
  24.  * @version 1.1, July 24, 1997
  25.  * @author Symantec
  26.  */
  27. public class ToolBarSpacer extends Canvas
  28. {
  29.     /**
  30.      * Create a ToolBarSpacer.  It's default space value is 10 pixels.
  31.      */
  32.     public ToolBarSpacer()
  33.     {
  34.         space = 10;
  35.     }
  36.  
  37.     /**
  38.      * Sets the size of the space the spacer will occupy in pixels.
  39.      * @param value the size of the space in pixels
  40.      * @exception PropertyVetoException
  41.      * if the specified property value is unacceptable
  42.      * @see #getSpace
  43.      */
  44.     public void setSpace(int value) throws PropertyVetoException
  45.     {
  46.         if (space != value)
  47.         {
  48.             Integer oldValue = new Integer(space);
  49.             Integer newValue = new Integer(value);
  50.  
  51.             vetos.fireVetoableChange("Space", oldValue, newValue);
  52.  
  53.             space = value;
  54.             changes.firePropertyChange("Space", oldValue, newValue);
  55.         }
  56.     }
  57.  
  58.     /**
  59.      * Gets the size of the space the spacer will occupy in pixels.
  60.      * @return the size of the space in pixels
  61.      * @see #setSpace
  62.      */
  63.     public int getSpace()
  64.     {
  65.         return space;
  66.     }
  67.  
  68.     /**
  69.      * Returns the recommended dimensions to properly display this component.
  70.      * This is a standard Java AWT method which gets called to determine
  71.      * the recommended size of this component.
  72.      *
  73.      * @return The width is the Space value and the height is the tallest component
  74.      * the ToolBarPanel, if the parent ToolBarPanel's Orientation is HORIZONTAL.
  75.      * The height is the Space value and the width is the widest component
  76.      * the ToolBarPanel, if the parent ToolBarPanel's Orientation is VERTICAL.
  77.      *
  78.      * NOTE: If the parent of this class is not an instance of ToolBarPanel, this will
  79.      * return 10,10.
  80.      *
  81.      * @see #setSpace
  82.      * @see #getSpace
  83.      * @see #getMinimumSize
  84.      */
  85.     public synchronized Dimension getPreferredSize()
  86.     {
  87.         //We assume that we will be in a ToolBarPanel and since it has a java.awt.Panel
  88.         //in it, we have to get the parent of the parent to actually get the ToolBarPanel.
  89.         Component parent = getParent();
  90.         if(parent != null)
  91.             parent = parent.getParent();
  92.  
  93.         if (parent != null && parent instanceof ToolBarPanel)
  94.         {
  95.                     Dimension s = new Dimension(0, 0);
  96.  
  97.             Component[] list = ((ToolBarPanel)parent).getComponents();
  98.  
  99.             switch(((ToolBarPanel)parent).getOrientation())
  100.             {
  101.                 case ToolBarPanel.HORIZONTAL:
  102.                     s.width = space;
  103.                     for (int i = 0; i < list.length; ++i)
  104.                     {
  105.                         Component c = list[i];
  106.                         if (!(c instanceof ToolBarSpacer))
  107.                             s.height = Math.max(s.height, c.size().height);
  108.                     }
  109.                     break;
  110.                 case ToolBarPanel.VERTICAL:
  111.                     s.height = space;
  112.                     for (int i = 0; i < list.length; ++i)
  113.                     {
  114.                         Component c = list[i];
  115.                         if (!(c instanceof ToolBarSpacer))
  116.                             s.width = Math.max(s.width, c.size().width);
  117.                     }
  118.                     break;
  119.             }
  120.  
  121.             return s;
  122.         }
  123.         else
  124.             return new Dimension(10, 10);
  125.     }
  126.  
  127.     /**
  128.      * @deprecated
  129.      * @see #getPreferredSize
  130.      */
  131.     public synchronized Dimension preferredSize()
  132.     {
  133.         return getPreferredSize();
  134.     }
  135.  
  136.     /**
  137.      * Returns the minimum dimensions to properly display this component.
  138.      * This is a standard Java AWT method which gets called to determine
  139.      * the minimum size of this component.
  140.      *
  141.      * In this case the minimum size is the same as the preferred size.
  142.      *
  143.      * @see #getPreferredSize
  144.      */
  145.     public synchronized Dimension getMinimumSize()
  146.     {
  147.         return preferredSize();
  148.     }
  149.  
  150.     /**
  151.      * @deprecated
  152.      * @see #getMinimumSize
  153.      */
  154.     public synchronized Dimension minimumSize()
  155.     {
  156.         return getMinimumSize();
  157.     }
  158.  
  159.     /**
  160.      * Is the specified space size valid?
  161.      * @param spaceSize the value to test
  162.      * @return true if the parameter is greater or equal to zero.
  163.      * @see #setSpace
  164.      * @see #getSpace
  165.      */
  166.     public boolean isValidSpace(int spaceSize)
  167.     {
  168.         return (spaceSize >= 0);
  169.     }
  170.  
  171.     /**
  172.      * Tells this component that it has been added to a container.
  173.      * This is a standard Java AWT method which gets called by the AWT when
  174.      * this component is added to a container. Typically, it is used to
  175.      * create this component's peer.
  176.      *
  177.      * It has been overridden here to hook-up event listeners.
  178.      *
  179.      * @see #removeNotify
  180.      */
  181.     public synchronized void addNotify()
  182.     {
  183.         super.addNotify();
  184.         errors = ResourceBundle.getBundle("symantec.itools.resources.ErrorsBundle");
  185.  
  186.         //Hook up listeners
  187.         if (veto == null)
  188.         {
  189.             veto = new Veto();
  190.             addSpaceListener(veto);
  191.         }
  192.     }
  193.  
  194.     /**
  195.      * Tells this component that it is being removed from a container.
  196.      * This is a standard Java AWT method which gets called by the AWT when
  197.      * this component is removed from a container. Typically, it is used to
  198.      * destroy the peers of this component and all its subcomponents.
  199.      *
  200.      * It has been overridden here to unhook event listeners.
  201.      *
  202.      * @see #addNotify
  203.      */
  204.     public synchronized void removeNotify()
  205.     {
  206.         //Unhook listeners
  207.         if (veto != null)
  208.         {
  209.             removeSpaceListener(veto);
  210.             veto = null;
  211.         }
  212.  
  213.         super.removeNotify();
  214.     }
  215.  
  216.     /**
  217.      * Adds a listener for all event changes.
  218.      * @param listener the listener to add.
  219.      * @see #removePropertyChangeListener
  220.      */
  221.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  222.     {
  223.         //super.addPropertyChangeListener(listener);
  224.         changes.addPropertyChangeListener(listener);
  225.     }
  226.  
  227.     /**
  228.      * Removes a listener for all event changes.
  229.      * @param listener the listener to remove.
  230.      * @see #addPropertyChangeListener
  231.      */
  232.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  233.     {
  234.         //super.removePropertyChangeListener(listener);
  235.         changes.removePropertyChangeListener(listener);
  236.     }
  237.  
  238.     /**
  239.      * Adds a vetoable listener for all event changes.
  240.      * @param listener the listener to add.
  241.      * @see #removeVetoableChangeListener
  242.      */
  243.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  244.     {
  245.         //super.addVetoableChangeListener(listener);
  246.         vetos.addVetoableChangeListener(listener);
  247.     }
  248.  
  249.     /**
  250.      * Removes a vetoable listener for all event changes.
  251.      * @param listener the listener to remove.
  252.      * @see #addVetoableChangeListener
  253.      */
  254.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  255.     {
  256.         //super.removeVetoableChangeListener(listener);
  257.         vetos.removeVetoableChangeListener(listener);
  258.     }
  259.  
  260.     /**
  261.      * Adds a listener for Space changes.
  262.      * @param listener the listener to add.
  263.      * @see #removeSpaceListener
  264.      */
  265.     public synchronized void addSpaceListener(PropertyChangeListener listener)
  266.     {
  267.         changes.addPropertyChangeListener("Space", listener);
  268.     }
  269.  
  270.     /**
  271.      * Removes a listener for Space changes.
  272.      * @param listener the listener to remove.
  273.      * @see #addSpaceListener
  274.      */
  275.     public synchronized void removeSpaceListener(PropertyChangeListener listener)
  276.     {
  277.         changes.removePropertyChangeListener("Space", listener);
  278.     }
  279.  
  280.     /**
  281.      * Adds a vetoable listener for Space changes.
  282.      * @param listener the listener to add.
  283.      * @see #removeSpaceListener
  284.      */
  285.     public synchronized void addSpaceListener(VetoableChangeListener listener)
  286.     {
  287.         vetos.addVetoableChangeListener("Space", listener);
  288.     }
  289.  
  290.     /**
  291.      * Removes a vetoable listener for Space changes.
  292.      * @param listener the listener to remove.
  293.      * @see #addSpaceListener
  294.      */
  295.     public synchronized void removeSpaceListener(VetoableChangeListener listener)
  296.     {
  297.         vetos.removeVetoableChangeListener("Space", listener);
  298.     }
  299.  
  300.     /**
  301.      * This is the PropertyChangeEvent handling inner class for the constrained Space property.
  302.      * Handles vetoing Spaces that are not valid.
  303.      */
  304.     class Veto implements VetoableChangeListener
  305.     {
  306.         /**
  307.          * This method gets called when an attempt to change the constrained Space property is made.
  308.          * Ensures the given Space is valid.
  309.          *
  310.          * @param     e a <code>PropertyChangeEvent</code> object describing the
  311.          *             event source and the property that has changed.
  312.          * @exception PropertyVetoException if the recipient wishes the property
  313.          *              change to be rolled back.
  314.          */
  315.         public void vetoableChange(PropertyChangeEvent e) throws PropertyVetoException
  316.         {
  317.             int i = ((Integer)e.getNewValue()).intValue();
  318.             if (!isValidSpace(i))
  319.             {
  320.                 throw new PropertyVetoException(errors.getString("InvalidSpace") + i, e);
  321.             }
  322.         }
  323.     }
  324.  
  325.     /**
  326.      * The size of the space the spacer will occupy in pixels..
  327.      */
  328.     protected int space;
  329.  
  330.     /**
  331.      * Error strings.
  332.      */
  333.     transient protected ResourceBundle errors;
  334.  
  335.     private Veto veto = null;
  336.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  337.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  338. }
  339.  
  340.